<template>
    <div class="applications-container">
      <div class="header">
        <div class="left">
          <h2>我的申请</h2>
        </div>
        <div class="right">
          <el-select
            v-model="statusFilter"
            placeholder="申请状态"
            clearable
            @change="handleStatusChange"
            class="status-filter"
          >
            <el-option label="待审核" value="PENDING" />
            <el-option label="已通过" value="APPROVED" />
            <el-option label="已拒绝" value="REJECTED" />
          </el-select>
        </div>
      </div>
  
      <el-card class="table-card">
        <el-table
          v-loading="loading"
          :data="applications"
          border
          stripe
          style="width: 100%"
        >
          <el-table-column prop="id" label="ID" width="80" />
          <el-table-column prop="position.title" label="申请岗位" min-width="150" />
          <el-table-column prop="position.teacher.name" label="指导教师" width="120" />
          <el-table-column prop="createTime" label="申请时间" width="180">
            <template #default="{ row }">
              {{ formatDate(row.createTime) }}
            </template>
          </el-table-column>
          <el-table-column prop="status" label="审核状态" width="100">
            <template #default="{ row }">
              <el-tag :type="getStatusType(row.status)">
                {{ getStatusText(row.status) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="reviewComments" label="审核意见" min-width="150" />
          <el-table-column label="操作" width="100" fixed="right">
            <template #default="{ row }">
              <el-button
                v-if="row.status === 'PENDING'"
                type="danger"
                size="small"
                @click="handleCancel(row)"
              >
                取消
              </el-button>
            </template>
          </el-table-column>
        </el-table>
  
        <div v-if="!loading && (!applications || applications.length === 0)" class="empty-data">
          <el-empty description="暂无申请" />
        </div>
  
        <div class="pagination">
          <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :total="total"
            :page-sizes="[10, 20, 50]"
            layout="total, sizes, prev, pager, next"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
    </div>
  </template>
  
  <script setup>
  import { ref, onMounted } from 'vue'
  import { ElMessage, ElMessageBox } from 'element-plus'
  import { getMyApplications, cancelApplication } from '@/api/student/jobApplication'
  import { formatDate } from '@/utils/format'
  
  // 表格数据
  const loading = ref(false)
  const applications = ref([])
  const total = ref(0)
  const currentPage = ref(1)
  const pageSize = ref(10)
  const statusFilter = ref('')
  
  // 获取申请列表
  const fetchApplications = async () => {
    try {
      loading.value = true
      const response = await getMyApplications({
        page: currentPage.value - 1,
        size: pageSize.value,
        status: statusFilter.value
      })
      
      if (response) {
        applications.value = response.content
        total.value = response.totalElements
      }
    } catch (error) {
      console.error('Failed to fetch applications:', error)
      ElMessage.error('获取申请列表失败')
    } finally {
      loading.value = false
    }
  }
  
  // 处理状态筛选
  const handleStatusChange = () => {
    currentPage.value = 1
    fetchApplications()
  }
  
  // 处理分页
  const handleSizeChange = (val) => {
    pageSize.value = val
    fetchApplications()
  }
  
  const handleCurrentChange = (val) => {
    currentPage.value = val
    fetchApplications()
  }
  
  // 取消申请
  const handleCancel = (row) => {
    ElMessageBox.confirm('确定要取消该申请吗？', '提示', {
      type: 'warning'
    }).then(async () => {
      try {
        await cancelApplication(row.id)
        ElMessage.success('取消成功')
        fetchApplications()
      } catch (error) {
        console.error('Cancel failed:', error)
        ElMessage.error('取消失败')
      }
    })
  }
  
  // 获取状态类型
  const getStatusType = (status) => {
    switch (status) {
      case 'PENDING':
        return 'warning'
      case 'APPROVED':
        return 'success'
      case 'REJECTED':
        return 'danger'
      default:
        return 'info'
    }
  }
  
  // 获取状态文本
  const getStatusText = (status) => {
    switch (status) {
      case 'PENDING':
        return '待审核'
      case 'APPROVED':
        return '已通过'
      case 'REJECTED':
        return '已拒绝'
      default:
        return '未知'
    }
  }
  
  // 在组件挂载时获取数据
  onMounted(() => {
    fetchApplications()
  })
  </script>
  
  <style scoped>
  .applications-container {
    padding: 20px;
  }
  
  .header {
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .right {
    display: flex;
    gap: 16px;
  }
  
  .status-filter {
    width: 120px;
  }
  
  .table-card {
    margin-bottom: 20px;
  }
  
  .pagination {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
  
  .empty-data {
    padding: 40px 0;
    text-align: center;
  }
  </style> 